/**
* ...
* @author Default
* @version 0.1
*/

package com.crlclr.ui {

	import com.crl.managers.ResizeableManager;
	import com.crlclr.managers.*;
	import com.crlclr.utils.*;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class QToolTip extends Sprite
	{
		private var _ruta:Sprite;
		private var _boton:Object;
		private var _clip:Sprite;
		private var _fondo:Shape;
		private var _texto:TextField;
		private var _formato:TextFormat;
		private var _sombra:Sombra;
		private var _delay:DelayAction;
		
		private var padding:Padding;
		
		private var _stage:Stage;
		public function QToolTip(boton:*, texto:String)
		{
			_ruta = boton.parent;
			_boton = boton;
			_clip = new Sprite();
			_clip.x = _boton.x;
			_clip.y = _boton.y;
			_clip.visible = false;
			_ruta.addChild(_clip);

			iniFondo();
			iniTexto();
			iniEvents();
			setTexto(texto);
			
			
			_stage=ResizeableManager.getInstance().stage;
		}

		private function iniFondo():void
		{
			_fondo = new Shape();
			_fondo.x = _fondo.y = 0;
			_sombra = new Sombra(_fondo);
			_clip.addChild(_fondo);
		}
		private function iniTexto():void
		{
			_texto = new TextField();
			_texto.mouseEnabled=false;
			_texto.autoSize=TextFieldAutoSize.LEFT;
			_formato = QTextFormat.defaultTextFormat;
			_formato.color = 0x999999;
			_texto.defaultTextFormat = _formato;
			_texto.x = 5;
			_texto.y = 2;
			_clip.addChild(_texto);
		}

		public function setTexto(texto:String):void
		{
			_texto.text = texto;
			updateFondo(_texto.textWidth+15,_texto.textHeight+10);
		}
		
		private function updateFondo(qWidth:int,qHeight:int):void
		{
			this.padding = new Padding(qWidth, qHeight);
			_fondo.graphics.clear();
			_fondo.graphics.beginFill(0xFFFFFF);
			_fondo.graphics.drawRoundRect(0, 0, qWidth, qHeight,8,8);
			_fondo.graphics.endFill();
		}
		
		private function iniEvents():void
		{
			_boton.addEventListener(MouseEvent.MOUSE_OVER, mouse_over);
			_boton.addEventListener(MouseEvent.MOUSE_OUT, mouse_out);
		}

		
		private function mouse_over(event:MouseEvent):void
		{
			_boton.addEventListener(MouseEvent.MOUSE_MOVE, mouse_move);
			_delay = new DelayAction(1,true,this,"mouse_over2");
		}
		public function mouse_over2():void
		{
			_clip.visible = true;
		}
		private function mouse_move(event:MouseEvent):void
		{
			_clip.y =_ruta.mouseY+20;
			
			
			if(_stage){
				if(_stage.mouseX>_stage.stageWidth/2){
					_clip.x =_ruta.mouseX-(padding.padX+10);
					return;
				}
			}
			_clip.x =_ruta.mouseX+10 ;
		}
		private function mouse_out(event:MouseEvent):void
		{
			_boton.removeEventListener(MouseEvent.MOUSE_MOVE, mouse_move);
			_clip.visible = false;
			_delay.reset();
		}
		
	}

}
